import yaml
import uvicorn
from fastapi import FastAPI
path ="C:/Users/Administrator/Desktop/新建文件夹/application.yaml"#括号里填yaml的绝对路径
def load_yaml(name):
    ##完成向yaml的写入
    with open(path,'r',encoding="utf-8")as f:
        yaml_dict = yaml.load(f, Loader=yaml.Loader)
    template_list=(yaml_dict["autoreply"]["group"]["command"])
    template_list.append(name)
    with open(path,'w',encoding="utf-8")as f:
        yaml.dump(yaml_dict,f,allow_unicode=True)

def new_template(nt_name,nt_sth):
    ##完成tmpl文件的生成
    filename = (f'template/command.group.{nt_name}.tmpl')
    with open (filename,'w',encoding="utf-8") as file_object:
        file_object.write(f'{nt_sth}')


app = FastAPI()
@app.get('/xzmb/')
async def make_new_file(nname:str,nsth:str):  ##基于fastapi的api
    new_template(nname,nsth)
    load_yaml(nname)
    return(f"模板{nname}已录入,请使用'/'{nname}测试或使用")

if __name__ == '__main__':
    uvicorn.run(app=app)

